<#assign module="partner"/>
<#assign ctx=request.contextPath />
<@override name="header">
<link rel="stylesheet" href="${ctx}/assets/css/datetimepicker.css">
</@override>
<@override name="body">
<div class="row">
	<div class="col-xs-10">
		<form class="form-inline" role="form">
	 		<div class="form-group">
                <input class="form-control" id="startTime" name="startTime" value="${startTime?if_exists}" placeholder="开始时间" style="max-width:106px;" readonly="readonly" type="text" >
            </div>
            <div class="form-group">
                <input class="form-control" id="endTime" name="endTime" value="${endTime?if_exists}" placeholder="结束时间" style="max-width:106px;" readonly="readonly" type="text" >
            </div>
            <div class="form-group">
	            <#if intents?? &&  categoryAlias=="weihu">
				<select class="form-control" id="intentId" name="intentId"/>
	                	<option value="" selected="selected">--全部--</option>
	            	<#list intents as intent>
		                <option value="${intent.id}" <#if intentId ?? && intentId==intent.id> selected="selected"</#if>>${intent.name}</option>
	                </#list>
	        	</select>
	            </#if>
		  	</div>
		  	<div class="form-group">
		  		<input type="text" class="form-control" style="width:250px;" name="param" value="${param?if_exists}" placeholder="输入商家电话、商家名称、地址">
		  	</div>
		  	<button type="submit" class="btn btn-default"><i class="icon-search"></i>&nbsp;搜索</button>
		</form>
  	</div>
	<div class="col-xs-2">
	  		<#if categoryAlias=="weihu">
	  		<button id="queryIntent" class="button button-rounded button-flat-blue"><i class="icon-plus"></i>&nbsp;分类管理</button>
	  		</#if>
	  		<button class="pull-right button button-flat-blue" 
	  		onclick="window.location.href='${ctx}/partner/toAdd/${categoryAlias}';"><i class="icon-plus"></i>&nbsp;添加商家</button>
  	</div>
</div>
<div class="row margin-top">
	<div class="col-xs-12">
	<div class="panel panel-primary">
	<div class="table-responsive">
		<table class="table table-hover table-striped">
			<thead>
			<tr style="font-size:14px;">
				<th>商家名称</th>
				<#if categoryAlias=="weihu">
				<th>分类</th>
				</#if>
				<th>电话</th>
				<th>地址</th>
				<th>合作时间</th>
				<th>所在省份</th>
				<th>所在城市</th>
				<th>所在区县</th>
				<th>操作</th>
			</tr>
			</thead>
			<tbody>
				<#if (page.content)?? >
			  	<#list page.content as partner>
		  			<tr>
						<td >${partner.name?if_exists}</td>
						<#if categoryAlias=="weihu">
						<td >${partner.intentName?if_exists}</td>
						</#if>
						<td >${partner.phone?if_exists}</td>
						<td >${partner.address?if_exists}</td>
						<td >${partner.created?string('MM-dd HH:mm')}</td>
						<td >${partner.province?if_exists}</td>
						<td >${partner.city?if_exists}</td>
						<td >${partner.county?if_exists}</td>
						<td  align="center">
							<@shiro.hasPermission name="partnerUpdate">
							<a href="${ctx}/partner/edit/${partner.id}" class="btn-link">编辑</a>
							</@shiro.hasPermission>
							<@shiro.hasPermission name="partnerDelete">
							<a href="javascript:;" data-value="${partner.id}"  class="btn-link deletethis">删除</a>
							</@shiro.hasPermission>
							<a href="${ctx}/partner/detail/${partner.id}" class="btn-link">查看详情</a>
						</td>
					</tr>
				</#list>
				</#if>
			</tbody>
		</table>
		<#if page.totalElements??>
		<ul class="pagination pull-right pagination-sm">
			<li><a>共 ${page.totalElements} 条数据</a></li>
			<@pg.pager items=page.totalElements?number maxPageItems=20 export="currentPageNumber=pageNumber" url="${ctx}/partner/list/${categoryAlias}">
				<@pg.param name="intentId" value="${intentId?if_exists}"/>
				<@pg.param name="param" value="${param?if_exists}"/>
				<@pg.param name="startTime" value="${startTime?if_exists}"/>
				<@pg.param name="endTime" value="${endTime?if_exists}"/>
				<@pg.index>
					<@pg.first>
						<li><a href="${pageUrl}">&laquo;</a></li>
					</@pg.first>
					<@pg.prev>
						<li><a href="${pageUrl}">&lsaquo;</a></li>
					</@pg.prev>
					<@pg.pages>
						<#if  currentPageNumber == pageNumber >
							<li class="active"><a href="#">${pageNumber}</a></li>
						<#else>
							<li><a href="${pageUrl}">${pageNumber}</a></li>
						</#if>
					</@pg.pages>
					<@pg.next>
						<li><a href="${pageUrl}">&rsaquo;</a></li>
					</@pg.next>
					<@pg.last>
						<li><a href="${pageUrl}">&raquo;</a></li>
					</@pg.last>
				</@pg.index>
			</@pg.pager>
		</ul>
		</#if>
	</div>
	</div>
  </div>
</div>

<div class="modal fade" id="dialog_add">
   <div class="modal-dialog">
     <div class="modal-content">
    	<div class="modal-header">
	        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        	<h4 class="modal-title">添加分类</h4>
        </div>
	    <div class="modal-body">
				<form id="submit_form" action="#" class="form-horizontal" method="post">
		               <input type="hidden" id="categoryAlias" name="categoryAlias" value="${categoryAlias}"/>
		                <div class="form-group">
		                    <label class="col-sm-4 control-label">分类名称：</label>
		                    <div class="col-sm-6">
		                    	<input class="form-control" id="name"  name="name" placeholder="请输入分类名称"  type="text" value="" required />
		                    </div>
		                </div>
		                <div class="form-group">
		                    <label class="col-sm-4 control-label">分类别名：</label>
		                    <div class="col-sm-6">
		                    	<input class="form-control" id="alias" name="alias" placeholder="别名不能重复"  type="text" value="" required />
		                    </div>
		                </div>
		                <div class="form-group" style="margin-left:30px;">
			                <label class="col-sm-4 control-label"></label>
			                <input type="button" id="save" class="btn btn-primary" value="保存">
			                <input type="button" id="close_modal" class="btn btn-default" value="返回">
			            </div>
				</form>
			</div>
		</div>
	</div>
</div>


<div class="modal fade" id="dialog_modifyCash">
   <div class="modal-dialog">
     <div class="modal-content">
    	<div class="modal-header">
	        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        	<h4 class="modal-title">分类管理</h4>
        </div>
	    <div class="modal-body" style="max-height:600px;overflow:auto;">
	    		<@shiro.hasPermission name="intentAdd">
    			<button id="addIntent" class="pull-right button button-rounded button-flat-blue"><i class="icon-plus"></i>&nbsp;添加分类</button>
    			</@shiro.hasPermission>
	    		<table class="table  table-striped">
					<thead>
						<tr>
							<th class="col-xs-5" >分类名称</th>
							<th class="col-xs-5" >分类别名</th>
							<#if show??>
							<th class="col-xs-1">操作</th>
							</#if>
						</tr>
					</thead>
					<tbody>
					<#if intents??>
					<#list intents as intent>
						<tr>
						<td>
						<span class="intentName">${intent.name}</span>
						<input class="inputName" style="display:none;" value="${intent.name}"></input>
						</td>
						<td >${intent.alias}</td>
						<td  align="center">
							<@shiro.hasPermission name="intentUpdate">
							<a href="javascript:;" data-value="${intent.id}"  class="btn-link updateIntent">编辑</a>
							</@shiro.hasPermission>
							<@shiro.hasPermission name="intentDelete">
							<a href="javascript:;" data-value="${intent.id}"  class="btn-link deleteIntent">删除</a>
							</@shiro.hasPermission>
						</td>
						</tr>
					</#list>	
					</#if>
				    </tbody>
		 	  </table>
    		
     	</div>
     </div>
  </div>
</div>
</@override>
<@override name="footer">
<script src="${ctx}/assets/js/bootstrap-datetimepicker.min.js"></script>
<script src="${ctx}/assets/js/bootstrap-datetimepicker.zh-CN.js"></script>
<script type="text/javascript">
	
	var thisButton;
    var intentId;
	function deletePartner() {
	    $.post('${ctx}/partner/delete',{'id':thisButton.attr("data-value")},function(data){
	            if(data.success){
	            	thisButton.parent().parent().remove();
	                $.scojs_message('商家删除成功', $.scojs_message.TYPE_OK);
	                window.location.reload(true);
	            }else{
	                $.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
	            }
	    },'json');
	}
	function deleteIntent() {
	    $.post('${ctx}/partner/deleteIntent',{'id':thisButton.attr("data-value")},function(data){
	            if(data.success){
	            	thisButton.parent().parent().remove();
	                $.scojs_message('分类删除成功', $.scojs_message.TYPE_OK);
	                window.location.reload(true);
	            }else{
	                $.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
	            }
	    },'json');
	}
	$(document).ready(function(){
	
		$("#startTime").datetimepicker({ format: 'yyyy-mm-dd', language: 'zh-CN', minView: 'month', pickDate: true, pickTime: false, inputMask: false, autoclose: true });
		$("#endTime").datetimepicker({ format: 'yyyy-mm-dd', language: 'zh-CN', minView: 'month', pickDate: true, pickTime: false, inputMask: false, autoclose: true });
	 
	    $('#startTime').change(function(){
      	    var begin=new Date($('#startTime').val().replace(/-/g,"/"));
	        var end=new Date($('#endTime').val().replace(/-/g,"/"));
	   	    if(begin!=null&&end!=null&&begin-end>=0){
  				 $('#startTime').val("");
  				 $.scojs_message("开始日期要在结束日期之前!", $.scojs_message.TYPE_ERROR);
	      	}
		});
     	$('#endTime').change(function(){
	      	var begin=new Date($('#startTime').val().replace(/-/g,"/"));
		    var end=new Date($('#endTime').val().replace(/-/g,"/"));
	   	    if(begin!=null&&end!=null&&begin-end>=0){
			  $('#endTime').val("");
			  $.scojs_message("结束日期要在开始日期之后!", $.scojs_message.TYPE_ERROR);
	        }
		});
		
		
		//删除商家
	    $(".deletethis").click(function(){
	          thisButton = $(this);
	          var confirm = $.scojs_confirm({
	            content: "你真的要删除该商家吗？",
	            action:deletePartner
	          });
	          confirm.show();
	    });
	    
	    $(".deleteIntent").click(function(){
	          thisButton = $(this);
	          var confirm = $.scojs_confirm({
	            content: "你真的要删除分类吗？<span style='color:red;'>删除后此分类下面的商家都会被删除!!</span>",
	            action:deleteIntent
	          });
	          confirm.show();
	    });
	    
	    //修改分类
	    $(".updateIntent").click(function(){
	          $(this).parents("tr").find("span").css("display","none");
	          $(this).parents("tr").find("input").css("display","black");
	          intentId =$(this).attr("data-value");
	    });
	    $(".inputName").blur(function(){
	          $(this).css("display","none");
	          $(this).parents("tr").find("span").css("display","black");
	          var newName=$(this).val();
	          $.post('${ctx}/partner/updateIntent',{'id':intentId,'name':newName},function(data){
	            if(data.success){
	                $(this).html(newName);
	                $.scojs_message('分类修改成功', $.scojs_message.TYPE_OK);
	                setTimeout(function(){
					window.location.reload(true);
					}, 1000);
	            }else{
	                $.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
	            }
	   		  },'json');
	          
	    });
	    
		//添加分类
	    $("#addIntent").click(function(){
			$('#dialog_add').modal();
			$('#dialog_modifyCash').modal("hide");
		});
	    $("#queryIntent").click(function(){
			$('#dialog_modifyCash').modal();
		});
		$("#close_modal").click(function(){
			$('#dialog_add').modal("hide");
		});
		
		$("#save").click(function(){
					$.post('${ctx}/partner/createIntent',{
						categoryAlias:$("#categoryAlias").val(),
						name:$("#name").val(),
						alias:$("#alias").val()},
						function(data){
				           if(data.success){
					            $.scojs_message(data.message, $.scojs_message.TYPE_OK);
				                setTimeout(function(){
										window.location.reload(true);
								}, 1000);
					       }else{
				               $.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
				                setTimeout(function(){
									window.location.reload(true);
								}, 1000);
					       }
					    },'json');
			 });   
			    
		
	});
</script>
</@override>
<@extends name="../layout.ftl"/>